package com.sgb.kjwl.view.ui.activity.pick;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPickNoticeSearchBinding;
import com.sgb.kjwl.di.pick.DaggerPickComponent;
import com.sgb.kjwl.di.pick.PickModule;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.PickEvent;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickNoticeListAdapter;
import com.sgb.kjwl.view.widget.MyItemDecoration;
import com.sgb.kjwl.viewmodel.pick.PickNoticeSearchModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

public class PickNoticeSearchActivity extends AppActivity {

    private ActivityPickNoticeSearchBinding binding;
    private PickNoticeListAdapter adapter;

    @Inject
    PickNoticeSearchModel pickNoticeSearchModel;
    private boolean isSearching;
    private String searchName = "";

    public static void start(Context context){
        Intent intent = new Intent(context,PickNoticeSearchActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPickComponent.builder()
                .appComponent(appComponent)
                .pickModule(new PickModule(PickNoticeSearchActivity.this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_notice_search);
        binding.topLayout.ivBack.setOnClickListener(view -> finish());
        binding.topLayout.tvSearch.setOnClickListener(view -> {
            if (TextUtils.isEmpty(binding.topLayout.etView.getText().toString())) {
                MToast.showToast(PickNoticeSearchActivity.this, "请输入搜索内容");
                return ;
            }
            if (searchName.equals(binding.topLayout.etView.getText().toString())){
                MToast.showToast(PickNoticeSearchActivity.this, "请输入不同的搜索内容");
                return ;
            }
            searchName = binding.topLayout.etView.getText().toString();
            if(!TextUtils.isEmpty(searchName)){
                isSearching = true;
                EventBus.getDefault().postSticky(PickEvent.make().setMessageTag(EventConstant.EVENT_PICK_NOTICE_SEARCH_DATA).setMessageData(searchName));
            }
        });

        binding.noticeList.setVisibility(View.GONE);
        binding.empty.setVisibility(View.VISIBLE);

        binding.noticeList.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        adapter = new PickNoticeListAdapter(getActivity(), null);
        binding.noticeList.addItemDecoration(new MyItemDecoration(true));
        binding.noticeList.setAdapter(adapter);

        adapter.setOnItemClick(new AdapterOnItemClick<PreNoticeItemEntity>() {
            @Override
            public void onItemClick(PreNoticeItemEntity goodsInfoEntity3, int position) {
                //招标中
                if (goodsInfoEntity3.getTenderState() == 9) {
                    TenderDetailsActivity3.start(getActivity(), goodsInfoEntity3.getTenderNo() + "",0);
                } else {
                    PickNoticeDetailActivity.start(getActivity(), goodsInfoEntity3.getTenderNo() + "");
                }
            }
        });

        binding.topLayout.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.topLayout.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (isSearching) {
                    return true;
                }
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(binding.topLayout.etView.getText().toString())) {
                        MToast.showToast(PickNoticeSearchActivity.this, "请输入搜索内容");
                        return true;
                    }
                    if (searchName.equals(binding.topLayout.etView.getText().toString())){
                        MToast.showToast(PickNoticeSearchActivity.this, "请输入不同的搜索内容");
                        return true;
                    }
                    isSearching = true;
                    searchName = binding.topLayout.etView.getText().toString();
                    if (!TextUtils.isEmpty(searchName)) {
                        pickNoticeSearchModel.getBiddingListData(searchName);
                    }
                }
                return false;
            }
        });
    }

    public void getBiddingData(List<PreNoticeItemEntity> list) {
        adapter.setDatas(list);
        binding.noticeList.setVisibility(View.VISIBLE);
        binding.empty.setVisibility(View.GONE);
        isSearching = false;
        EventBus.getDefault().post(
                PickEvent.make().setMessageTag(EventConstant.EVENT_PICK_NOTICE_SEARCH_BIDING)
                        .setMessageData(list.size()));
    }

    public void empty() {
        binding.noticeList.setVisibility(View.GONE);
        binding.empty.setVisibility(View.VISIBLE);
        isSearching = false;
        EventBus.getDefault().postSticky(
                PickEvent.make().setMessageTag(EventConstant.EVENT_PICK_NOTICE_SEARCH_BIDING)
                        .setMessageData(0));
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(PickEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PICK_NOTICE_SEARCH_DATA) {
            if(isSearching){
                pickNoticeSearchModel.getBiddingListData((String) event.getMessageData());
            }
        }
    }

    public void getUrl(String url, String id) {
        WebViewX5HostActivity.start(getActivity(), url, id);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
